package com.zxw.mybatis;


import com.zxw.mybatis.domain.*;
import com.zxw.mybatis.domain.meters.*;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author :zhangxuewei
 * @date : 2023/3/4
 */
public class OracleResultMapUtil {


    /**
     * @param c
     * @return
     */
    public static List<String> getParamList(Class c) {
        List<String> propertys = new ArrayList<>();
        Field[] vos = c.getDeclaredFields();
        for (Field field : vos) {
            field.setAccessible(true);
            propertys.add(field.getName());
        }
        Class<?> superclass = c.getClass();
        while (superclass != null) {
            superclass = superclass.getSuperclass();
            if (superclass.getName().equals("java.lang.Object")) {
                break;
            }
            //获取父类的属性
            Field[] superField = superclass.getDeclaredFields();
            for (Field field : superField) {
                field.setAccessible(true);
                propertys.add(field.getName());
            }
        }
        return propertys;
    }

    private static final String ResultHead = "<resultMap type=\"resultMapType\" id=\"resultMapId\"> \n";
    private static final String Resultfoot = "</resultMap>";

    public static void getResultMap(Class c, String resultMapType, String resultMapId, String tableAliase) {
        String sqlcloumn = "";
        List<String> ps = getParamList(c);
        String resultMapBody = "";
        for (String pv : ps) {
            String p = pv;
            if (p.equals("serialVersionUID"))
                continue;
            p = String.valueOf(p.charAt(0)).toUpperCase().concat(p.substring(1));
            StringBuffer sb = new StringBuffer();
            Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
            Matcher matcher = pattern.matcher(p);
            while (matcher.find()) {
                String word = matcher.group();
                sb.append(word.toUpperCase());
                sb.append(matcher.end() == p.length() ? "" : "_");
            }
            sqlcloumn += (tableAliase + "." + sb + ",");
            //
            resultMapBody += "<result property=\"" + pv + "\"    column=\"" + sb.toString() + "\"    />\n";// sb.toString().toLowerCase()改成小写
        }
        String ResultHeadM = ResultHead;
        ResultHeadM = ResultHeadM.replaceAll("resultMapType", resultMapType);
        ResultHeadM = ResultHeadM.replace("resultMapId", resultMapId);
        ResultHeadM += resultMapBody;
        String ResultfootM = Resultfoot;
        ResultHeadM += ResultfootM;
        System.out.println(ResultHeadM);
        System.out.println(sqlcloumn);
    }


    public static void getClassProperty() {
        String str = "wdId //主键Id\n" +
                "wdAreaSn //线别\n" +
                "wdMoNumber //制令单号\n" +
                "wdModelCode //机种\n" +
                "wdProcessFace //生产面别（0：单面，1正面，2反面，3阴阳面）\n";
        String p = "";
        for (String r : str.split("\n")) {
            String[] a = r.split(" ");
            p += a[1] + "\n";
            p += "private String " + a[0] + ";" + "\n";
        }
        System.out.println(p);
    }

    public static void main(String[] args) {
        int status = 1;// 0-转换类属性和注释 1-转换resultMap
        if (status == 0) {
            getClassProperty();
        } else if (status == 1) {
            getResultMap(Inspect.class, "", "", "T1");
        }
    }
}